package de.clapp.backend.requests;

import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import de.clapp.backend.database.Conn;
import de.clapp.backend.database.Queries;
import de.clapp.client.order.OrderModel;
import de.clapp.data.Artikel;
import de.clapp.data.Order;

public class OrderRequest implements ServerRequest
{

    /**
     * 
     */
    private static final long serialVersionUID = -8833161646408970590L;

    private Order item;

    @Override
    public Type getReturnType()
    {
        return Order.class;
    }

    @Override
    public Object getItem()
    {
        return this.item;
    }

    @Override
    public void setItem(Object item)
    {
        this.item = (Order) item;
    }

    @Override
    public Object execute()
    {
        /*
         * Bestellung in die Datenbank schreiben
         */
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int currentOrderNumber = Integer.MIN_VALUE;
        try
        {
            con = Conn.getInstance().clientCon;
            
            pstmt = con.prepareStatement(Queries.GET_BENUTZER_BY_ID);
            pstmt.setString(1, item.getBenutzer().getIdBenutzer());
            rs = pstmt.executeQuery();
            
            if(!rs.next())
            {
                pstmt = con.prepareStatement(Queries.INSERT_BENUTZER);
                pstmt.setString(1, item.getBenutzer().getIdBenutzer());
                pstmt.setString(2, "IMEI/USER_ID: "+item.getBenutzer().getIdBenutzer());
                pstmt.execute();
                System.out.println("Benutzer angelegt");
            }
            
            pstmt = con.prepareStatement(Queries.INSERT_ORDER, Statement.RETURN_GENERATED_KEYS);
            pstmt.setInt(1, item.getTisch().getTischID());
            pstmt.setString(2, item.getBenutzer().getIdBenutzer());
            pstmt.setTimestamp(3, new Timestamp(Calendar.getInstance().getTime().getTime()));
            pstmt.execute();

            rs = pstmt.getGeneratedKeys();

            if (rs.next())
            {
                currentOrderNumber = rs.getInt(1);
                item.setOrdernumber(currentOrderNumber);
                pstmt = con.prepareStatement(Queries.INSERT_ORDER_IN_BPOS);
                Set<Entry<Artikel, Integer>> set = item.getOrder().entrySet();
                Iterator<Entry<Artikel, Integer>> iterator = set.iterator();
                while (iterator.hasNext())
                {
                    Entry<Artikel, Integer> entry = iterator.next();
                    pstmt.setInt(1, entry.getValue());
                    pstmt.setInt(2, currentOrderNumber);
                    pstmt.setInt(3, entry.getKey().getIdArtikel());
                    pstmt.execute();
                }
            }

            // Lisa bescheid sagen, dass sie ihre Anzeige aktualisieren muss
            OrderModel.getInstance().fillOrders();
            
            
            con.commit();
            pstmt.close();
            rs.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(pstmt != null)
            {
                try
                {
                    pstmt.close();
                }
                catch (SQLException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(rs != null)
            {
                try
                {
                    rs.close();
                }
                catch (SQLException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return item;
    }

}
